﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace BPMS.Utility.Web
{
    public class ClientUtility
    {
        /// <summary>
        /// 获得用户IP
        /// </summary>
        public static string GetClientIP()
        {
            string clientIP = "1.1.1.1";
            if (HttpContext.Current == null
                || HttpContext.Current.Request == null)
            {
                return clientIP;
            }
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null)
            {
                clientIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
            else
            {
                clientIP = HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"].ToString();
            }
            if (clientIP.Length > 15)
            {
                return clientIP;
            }
            else
            {
                string[] temp = clientIP.Split('.');
                if (temp.Length == 4)
                {
                    for (int i = 0; i < temp.Length; i++)
                    {
                        if (temp[i].Length > 3)
                            return clientIP;
                    }
                }
                else
                {
                    return clientIP;
                }
            }
            return clientIP;
        }

        /// <summary>
        /// 获取客户端浏览器信息
        /// </summary>
        /// <returns></returns>
        public static string GetClientBrowser()
        {
            string browser = "unknown";
            if (HttpContext.Current == null
                || HttpContext.Current.Request == null)
            {
                return browser;
            }
            HttpBrowserCapabilities httpBrowser = HttpContext.Current.Request.Browser;
            if (null != httpBrowser)
            {
                browser = httpBrowser.Browser + " " + httpBrowser.Version;
            }
            return browser;
        }

        /// <summary>
        /// 返回客户端浏览器版本
        /// 如果是IE类型，返回版本数字
        /// 如果不是IE类型，返回-1
        /// </summary>
        /// <returns>一位数字版本号</returns>
        public static int GetClientBrowserVersion()
        {
            if (HttpContext.Current == null
                || HttpContext.Current.Request == null)
            {
                return 0;
            }
            string USER_AGENT = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];
            if (USER_AGENT.IndexOf("MSIE") < 0)
                return -1;
            string version = USER_AGENT.Substring(USER_AGENT.IndexOf("MSIE") + 5, 1);
            if (!RegexExpress.RegexUtility.IsMatch(version, RegexExpress.RegexPattern.Numeric))
                return -1;
            return Convert.ToInt32(version);
        }

        /// <summary>
        /// 获取客户端系统版本信息
        /// </summary>
        /// <returns></returns>
        public static string GetClientOS()
        {
            if (HttpContext.Current == null
                || HttpContext.Current.Request == null)
            {
                return "unknown";
            }
            string Agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];

            if (Agent.IndexOf("NT 4.0") > 0)
            {
                return "Windows NT ";
            }
            else if (Agent.IndexOf("NT 5.0") > 0)
            {
                return "Windows 2000";
            }
            else if (Agent.IndexOf("NT 5.1") > 0)
            {
                return "Windows XP";
            }
            else if (Agent.IndexOf("NT 5.2") > 0)
            {
                return "Windows 2003";
            }
            else if (Agent.IndexOf("NT 6.0") > 0)
            {
                return "Windows Vista";
            }
            else if (Agent.IndexOf("WindowsCE") > 0)
            {
                return "Windows CE";
            }
            else if (Agent.IndexOf("NT") > 0)
            {
                return "Windows NT ";
            }
            else if (Agent.IndexOf("9x") > 0)
            {
                return "Windows ME";
            }
            else if (Agent.IndexOf("98") > 0)
            {
                return "Windows 98";
            }
            else if (Agent.IndexOf("95") > 0)
            {
                return "Windows 95";
            }
            else if (Agent.IndexOf("Win32") > 0)
            {
                return "Win32";
            }
            else if (Agent.IndexOf("Linux") > 0)
            {
                return "Linux";
            }
            else if (Agent.IndexOf("SunOS") > 0)
            {
                return "SunOS";
            }
            else if (Agent.IndexOf("Mac") > 0)
            {
                return "Mac";
            }
            else if (Agent.IndexOf("Linux") > 0)
            {
                return "Linux";
            }
            else if (Agent.IndexOf("Windows") > 0)
            {
                return "Windows";
            }
            return "unknown";
        }

        /// <summary>
        /// 是否手机端请求
        /// </summary>
        /// <returns></returns>
        public static bool IsMobileRequest()
        {
            if (HttpContext.Current == null
                || HttpContext.Current.Request == null)
            {
                return false;
            }
            HttpRequest request = HttpContext.Current.Request;
            //FIRST TRY BUILT IN ASP.NT CHECK
            if (request.Browser.IsMobileDevice)
            {
                return true;
            }
            //THEN TRY CHECKING FOR THE HTTP_X_WAP_PROFILE HEADER
            if (request.ServerVariables["HTTP_X_WAP_PROFILE"] != null)
            {
                return true;
            }
            //THEN TRY CHECKING THAT HTTP_ACCEPT EXISTS AND CONTAINS WAP
            if (request.ServerVariables["HTTP_ACCEPT"] != null &&
                request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap"))
            {
                return true;
            }
            //AND FINALLY CHECK THE HTTP_USER_AGENT
            //HEADER VARIABLE FOR ANY ONE OF THE FOLLOWING
            if (request.ServerVariables["HTTP_USER_AGENT"] != null)
            {
                //Create a list of all mobile types
                string[] mobiles =
                    new[]
              {
                  "midp", "j2me", "avant", "docomo",
                  "novarra", "palmos", "palmsource",
                  "240x320", "opwv", "chtml",
                  "pda", "windows ce", "mmp/",
                  "blackberry", "mib/", "symbian",
                  "wireless", "nokia", "hand", "mobi",
                  "phone", "cdm", "up.b", "audio",
                  "SIE-", "SEC-", "samsung", "HTC",
                  "mot-", "mitsu", "sagem", "sony"
                  , "alcatel", "lg", "eric", "vx",
                  "NEC", "philips", "mmm", "xx",
                  "panasonic", "sharp", "wap", "sch",
                  "rover", "pocket", "benq", "java",
                  "pt", "pg", "vox", "amoi",
                  "bird", "compal", "kg", "voda",
                  "sany", "kdd", "dbt", "sendo",
                  "sgh", "gradi", "jb", "dddi",
                  "moto", "iphone"
              };

                //Loop through each item in the list created above
                //and check if the header contains that text
                foreach (string s in mobiles)
                {
                    if (request.ServerVariables["HTTP_USER_AGENT"].ToLower().Contains(s.ToLower()))
                    {
                        return true;
                    }
                }
            }

            return false;
        }
    }
}
